package com.example.frescostudy;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.frescostudy.Adapter.FrescoAdapter;
import com.facebook.common.logging.FLog;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private FrescoAdapter frescoAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        FLog.setMinimumLoggingLevel(FLog.WARN);
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void findView() {
        int[] colorResources = new int[]{R.color.color_1, R.color.color_2, R.color.color_3, R.color.color_4};
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swp_rl);
        swipeRefreshLayout.setColorSchemeResources(colorResources);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setColorSchemeResources(R.color.color_progress_bg);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view_fresco);
        linearLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        frescoAdapter = new FrescoAdapter();

        recyclerView.setAdapter(frescoAdapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();

                        }
                        Log.e("frc", "3000  over");
                        mHandler.sendEmptyMessage(1);

                    }
                }).start();

            }
        });
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == frescoAdapter.getItemCount()) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //刷新处理
                        }
                    }, 1000);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });

    }

    private int lastVisibleItem;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Log.e("frc", "swipeRefreshLayout.setRefreshing(false);");
                    swipeRefreshLayout.setRefreshing(false);
                    break;
            }
        }
    };

}
